/**
 * Player Manager
 */
function PlayerManager() {
	this.players = [];
}

var PLAYER_MANAGER = "PlayerManager";
var PLAYER_MANAGER_VERSION = "0.4";
function storePlayerManager(pm) {
	console.log("StorePlayerManager");
	var data = {
		version: PLAYER_MANAGER_VERSION,
		players: [],
	};
	for (var p in pm.players) {
		var player = pm.players[p];
		data.players.push({ id:      player.id,
							name:    player.name,
							color:   player.color,
							gfx:     player.gfx,
							systems: player.systems,
							type:    player.type });
	}
	LocalStorage.set(PLAYER_MANAGER, JSON.stringify(data));
}
function loadPlayerManager() {
	var pm = new PlayerManager();
	var data = JSON.parse(LocalStorage.get(PLAYER_MANAGER));
	if (data.version != PLAYER_MANAGER_VERSION) {
		LocalStorage.del(PLAYER_MANAGER);
		throw "PlayerManager is wrong version!";
	}
	pm.players = data.players;

	return pm;
}

var thePlayerManager = null;
function getPlayerManager() {
	if (!thePlayerManager) {
		if (LocalStorage.has(PLAYER_MANAGER)) {
			thePlayerManager = loadPlayerManager();
		} else {
			thePlayerManager = new PlayerManager();
			thePlayerManager.initPlayers();
			storePlayerManager(thePlayerManager);
		}
	}
	return thePlayerManager;
}

PlayerManager.prototype.store = function() {
	console.log("StorePlayerManager");
	storePlayerManager(this);
}

PlayerManager.prototype.initPlayers = function() {
	this.players = [];
	this.players.push({ id: "p0", name : "P0", color: "#808080", gfx: "gfx/p0", systems: [], type: "default" },
					  { id: "p1", name : "P1", color: "#FF0000", gfx: "gfx/p1", systems: [], type: "player" },
					  { id: "p2", name : "P2", color: "#FFFF00", gfx: "gfx/p2", systems: [], type: "npc" },
					  { id: "p3", name : "P3", color: "#00FF00", gfx: "gfx/p3", systems: [], type: "npc" },
					  { id: "p4", name : "P4", color: "#00FFFF", gfx: "gfx/p4", systems: [], type: "npc" },
					  { id: "p5", name : "P5", color: "#0000FF", gfx: "gfx/p5", systems: [], type: "npc" },
					  { id: "p6", name : "P6", color: "#FF00FF", gfx: "gfx/p6", systems: [], type: "npc" });
}

//PlayerManager.prototype.getDefaultPlayer = function() {
//	return this.getPlayer({type:"default"});
//}
//PlayerManager.prototype.getCurrentPlayer = function() {
//	return this.getPlayer({type:"player"});
//}

PlayerManager.prototype.addSystem = function(playerId, systemId) {
	if (!playerId) return;
	if (!systemId) return;

	var player = this.getPlayer({id: playerId});
	if (!player) return;

	if (player.systems.indexOf(systemId) < 0) {
		player.systems.push(systemId);
	}
}
PlayerManager.prototype.removeSystem = function(playerId, systemId) {
	if (!playerId) return;
	if (!systemId) return;

	var player = this.getPlayer({id: playerId});
	if (!player) return;

	var index = player.systems.indexOf(systemId);
	if (index >= 0) {
		player.systems.splice(index, 1);
	}
}

PlayerManager.prototype.getPlayer = function(filter) {
	var result = null;
	for (var p in this.players) {
		var player = this.players[p];
		if (typeof filter == "function" && filter(player)) {
			result = player;
			break;
		}

		if (typeof filter == "object") {
			var isOk = true;
			for (var key in filter) {
				if (filter[key] != player[key]) {
					isOk = false;
					break;
				}
			}
			if (isOk) {
				result = player;
				break;
			}
		}
	}
	return result;
}
PlayerManager.prototype.getPlayers = function(filter) {
	var result = [];
	for (var p in this.players) {
		var player = this.players[p];
		if (typeof filter == "function" && filter(player)) {
			result.push(player);
		}

		if (typeof filter == "object") {
			var isOk = true;
			for (var key in filter) {
				if (filter[key] != player[key]) {
					isOk = false;
					break;
				}
			}
			if (isOk) {
				result.push(player);
			}
		}
	}
	return result;
}

var TestPlayerManager = {};

TestPlayerManager.testPlayerWithId = function() {
	var pm = new PlayerManager();
	pm.initPlayers();
	assertNull(pm.getPlayer({id:"px"}), "testPlayerWithId(p0) null");
	assertNotNull(pm.getPlayer({id:"p1"}), "testPlayerWithId(p1) not null");
	assertEqual(pm.getPlayer({id:"p1"}).id, "p1", "testPlayerWithId(p1).id is p1");
	assertNotEqual(pm.getPlayer({id:"p1"}).id, "p0", "testPlayerWithId(p1).id is not p0");
}
TestPlayerManager.testPlayerWithName = function() {
	var pm = new PlayerManager();
	pm.initPlayers();
	assertNull(pm.getPlayer({name:"PX"}), "testPlayerWithName(P0) null");
	assertNotNull(pm.getPlayer({name:"P1"}), "testPlayerWithName(P1) not null");
	assertEqual(pm.getPlayer({name:"P1"}).name, "P1", "testPlayerWithName(P1).id is P1");
	assertNotEqual(pm.getPlayer({name:"P1"}).name, "P0", "testPlayerWithName(P1).id is not P0");
}

TestPlayerManager.test = function() {
	try {
		this.testPlayerWithId();
		this.testPlayerWithName();
//		this.testGetLivePlayers();
//		this.testGetPlayerCount();
	} catch(e) {
		console.error(e);
	}
}
